/*
 *  Cell.cpp
 *  GraphPaper
 *
 *  Created by Olivier Namet on 30/03/2010.
 *  Copyright 2010 Home. All rights reserved.
 *
 */

#include "Cell.h"
#include "Port.h"
#include <iostream>

//#include "Vector.h"

#include <sstream>

#include <math.h>

using namespace std;

#define PI 3.14159265
#define cellR 1


int Cell::cellCount = 0;


Cell::Cell(std::string s)
{
	Cell(s,0,0);
}

Cell::Cell(float x, float y)
{
	Cell("",x,y);
}

Cell::Cell(std::string s, Pos *p)
{
	Cell(s,p->getX(),p->getY());
}

Cell::Cell(std::string s, float x, float y)
{
	
	name = s;
	pos.setX(x);
	pos.setY(y);
	
	setId(cellCount);
	cellCount++;
	
	cout << "Created Cell #" << getId() << " with name: " << s << " and pos: " << x << "," << y << endl;
	
}

Cell::~Cell()
{
	
}

void Cell::addPort()
{
	
	//find smallest available number
/*
	int sm = 0;
	
	for(int i = 0 ; i < ports.size() ; i++)
	{
		std::stringstream ss;
		ss << sm;
		if(ports[i].getName()==ss.str())
		{
			sm++;
			i=0;
		}
	}
	
	std::stringstream ss;
	ss << sm;
	

*/
	
	//add the port to the ports vector with smallest available number

	//cout << "*Adding Port " << ss.str() << " to cell at " << this->getX() << "," << this->getY() << endl;

	//cout << "PORT CREATIONG THIS:  " << this << endl;
	
	//ss.str()
	Port *p = new Port("a",this);


	
	//cout << "Adding Port " << p->getName() << " to cell at " << p->getCell()->getX() << " , " << p->getCell()->getY() << endl;
	
	this->ports.push_back(*p);

	//cout << "PORT CREATION P->GETCELL  " << p->getCell() << endl;

	//this->ports.push_back(Port(ss.str(),this));

	this->updatePortPositions();

	//delete p;
}


void Cell::removePort(int i)
{
	std::vector<Port>::iterator it = ports.begin();
	ports.erase(it);
	
	this->updatePortPositions();
}




void Cell::updatePortPositions()
{
	//drawCircle(graph.cells[i].getX() + sin((360*j/graph.cells[i].ports.size())*PI/180)*0.5,  graph.cells[i].getY() +cos((360*j/graph.cells[i].ports.size())*PI/180)*0.5, 0.1, 30, black, white);
	
	for(int i = 0 ; i < ports.size() ; i++)
	{
		
		Pos *p = new Pos(sin((360*i/ports.size())*PI/180)*cellR,cos((360*i/ports.size())*PI/180)*cellR);
		
		Pos *p2 = new Pos(sin((360*i/ports.size())*PI/180)*(cellR+0.4),cos((360*i/ports.size())*PI/180)*(cellR+0.4));

		ports[i].setPos(p);
		ports[i].setLabellPos(p2);

		//delete p;
		//delete p2;
	}
	
	
}







